package com.suarte.webapp.action;

import com.suarte.core.Company;
import com.suarte.core.Contact;
import com.suarte.core.service.ContactManager;
import com.suarte.core.service.WorkOrderManager;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.appfuse.model.Role;
import org.appfuse.model.User;
import org.appfuse.service.GenericManager;

/**
 * @date   Dec 20, 2010
 * @author Ggutierrez
 */
public class WorkOrderList extends BasePage implements Serializable {

    private WorkOrderManager workOrderManager;
    private GenericManager<Company, Long> companyManager;
    private ContactManager contactManager;
    private Long id;
    private Long quotationId;
    private String description;
    private String user;
    private Date date;
    private List workOrders;
    private List<Company> companies;
    private Company company;
    private List<Contact> contacts;
    private Contact contact;
    private Boolean adminRole = false;
    private User paramUser;

    public void setWorkOrderManager(WorkOrderManager workOrderManager) {
        this.workOrderManager = workOrderManager;
    }

    public void setCompanyManager(GenericManager<Company, Long> companyManager) {
        this.companyManager = companyManager;
    }

    public void setContactManager(ContactManager contactManager) {
        this.contactManager = contactManager;
    }

    public WorkOrderList() {
        setSortColumn("id"); // sets the default sort column
    }

    public String execute(){
        HttpServletRequest request = getRequest();
        user = request.getRemoteUser();
        System.out.println("User " + user);

        User loginUser = userManager.getUserByUsername(user);
        for (Role role : loginUser.getRoles()) {
            if (role.getName().equals("ROLE_ADMIN")){
                user = null;
                adminRole = true;
            }
        }

        if (paramUser != null){
            user = paramUser.getUsername();
        }
        
        workOrders = sort(workOrderManager.findOrders(id, quotationId, description, user, date, company, contact));

        return "list";
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getQuotationId() {
        return quotationId;
    }

    public void setQuotationId(Long quotationId) {
        this.quotationId = quotationId;
    }

    public List getWorkOrders() {
        return workOrders;
    }

    public void setWorkOrders(List workOrders) {
        this.workOrders = workOrders;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public List<Company> getCompanies() {
        companies = companyManager.getAll();
        return companies;
    }

    public void setCompanies(List<Company> companies) {
        this.companies = companies;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public List<Contact> getContacts() {
        return contacts;
    }

    public void setContacts(List<Contact> contacts) {
        //contacts = contactManager.getAll();
        this.contacts = contacts;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public Boolean getAdminRole() {
        return adminRole;
    }

    public void setAdminRole(Boolean adminRole) {
        this.adminRole = adminRole;
    }

    public User getParamUser() {
        return paramUser;
    }

    public void setParamUser(User paramUser) {
        this.paramUser = paramUser;
    }

    public List<User> getUsers(){
        return userManager.getAll();
    }
}
